package com.popo.android;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.popo.android.provider.MyProvider;

public class ProviderTest extends Activity {
	ContentResolver cr;
	Cursor cursor;
	String table;
	SimpleCursorAdapter adapter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		cr = getContentResolver();
		setContentView(R.layout.main);

		ListView list = (ListView) findViewById(R.id.rslist);

		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == R.id.radio0) {
					table = "tet1";
				} else {
					table = "test2";

				}
				notifyCheckedChanged();
			}
		});
		final EditText et = (EditText) findViewById(R.id.ed);
		Button button = (Button) findViewById(R.id.intentbtn);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String val = et.getText().toString();
				Log.v("msg", "val[" + val + "]");

				if (val.length() > 0) {
					addData("test", val);
					notifyCheckedChanged();
				}
			}
		});

		cursor = getData();
		adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, cursor, new String[] {
						"_id", "name" }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		list.setAdapter(adapter);

	}

	protected void notifyCheckedChanged() {
		// TODO Auto-generated method stub

		if (cursor != null) {
			cursor.close();

		}

		cursor = getData();

		try {

			if (cursor.moveToFirst()) {
				Log.v("msg", "out _id[" + cursor.getInt(0) + "],name["
						+ cursor.getString(1) + "]");
			} else {
				Log.v("msg", "out sorry don't get data");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();

	}

	private Cursor getData() {
		// TODO Auto-generated method stub
		return cr.query(Uri.parse("content://" + MyProvider.MyUri + "/test"),
				null, " name like '%a%'", null, null);

	}

	private void addData(String table, String name) {
		// TODO Auto-generated method stub

		ContentValues cv = new ContentValues();
		cv.put("name", name);
		// cr.insert(MyProvider.MyUri, new String[]{"aaa"});
		Uri ur = cr.insert(
				Uri.parse("content://" + MyProvider.MyUri + "/test"), cv);

	}

}
